package com.cn.kabalifeuser.ui.service;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.config.SPLocationData;
import com.cn.kabalifeuser.ui.integral.activity.IntegralCommodityDetailsActivity;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.activity.NwsMsgActivity;
import com.cn.kabalifeuser.ui.my.adapter.AddressCheckAdapter;
import com.cn.kabalifeuser.ui.my.bean.AddressProvinceListBean;
import com.cn.kabalifeuser.ui.service.activity.BusinessHomeActivity;
import com.cn.kabalifeuser.ui.service.activity.MerchantsListActivity;
import com.cn.kabalifeuser.ui.service.activity.NewsInfoActivity;
import com.cn.kabalifeuser.ui.service.activity.RichTextH5Activity;
import com.cn.kabalifeuser.ui.service.activity.RichTextWebViewH5Activity;
import com.cn.kabalifeuser.ui.service.activity.TypeBusinessActivity;
import com.cn.kabalifeuser.ui.service.adapter.BusinessAdapter;
import com.cn.kabalifeuser.ui.service.adapter.RecommendAdapter;
import com.cn.kabalifeuser.ui.service.adapter.StrListAdapter;
import com.cn.kabalifeuser.ui.service.adapter.StrListAdapter2;
import com.cn.kabalifeuser.ui.service.adapter.TypeAdapter;
import com.cn.kabalifeuser.ui.service.bean.BannerListBean;
import com.cn.kabalifeuser.ui.service.bean.BusinessListBean;
import com.cn.kabalifeuser.ui.service.bean.CityIdBean;
import com.cn.kabalifeuser.ui.service.bean.IndexTypeListBean;
import com.cn.kabalifeuser.ui.service.bean.NewListBean;
import com.cn.kabalifeuser.ui.service.bean.StrListBean;
import com.cn.kabalifeuser.utils.GlideImageLoader;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.SPUtil;
import com.cn.kabalifeuser.view.PopupWindow.CommonPopupWindow;
import com.cn.kabalifeuser.view.PopupWindow.CommonUtil;
import com.cn.kabalifeuser.view.dialogView.ApplyShopDialog;
import com.flyco.roundview.RoundTextView;
import com.google.android.material.appbar.AppBarLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/2.
 * function:
 */
public class FragmentService extends LazyFragment<FragmentServiceView, FragmentServicePresenter> implements FragmentServiceView, CommonPopupWindow.ViewInterface {
    @BindView(R.id.tv_msg)
    RoundTextView tvMsg;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.banner_tj)
    Banner bannerTj;
    @BindView(R.id.fl_layout)
    FrameLayout flLayout;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.view_flipper)
    ViewFlipper viewFlipper;
    @BindView(R.id.ll_flipper)
    LinearLayout llFlipper;
    @BindView(R.id.rv_business_list)
    RecyclerView rvBusinessList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.rv_recommend_list)
    RecyclerView rvRecommendList;
    @BindView(R.id.rv_type_list)
    RecyclerView rvTypeList;
    @BindView(R.id.tv_city)
    TextView tvCity;

    //分类
    List<IndexTypeListBean.ResultBean> typeList;

    List<IndexTypeListBean.ResultBean> typeList5;//5条数据/
    int indexSign=1;//默认显示5个。

    TypeAdapter typeAdapter;
    ///商家列表
    List<BusinessListBean.ResultBean.DataBean> businessList;
    BusinessAdapter businessAdapter;
    //推荐专区
    List<BusinessListBean.ResultBean.DataBean> recommendList;
    RecommendAdapter recommendAdapter;


    BannerListBean bannerListBean;
    BannerListBean bannerTjListBean;
    NewListBean newsListBean;
    private String b_district = "拱墅区";
    private String b_city = "杭州市";

    CityIdBean cityIdBean;


    private CommonPopupWindow popupWindowAddress;
    List<AddressProvinceListBean.ResultBean> mListCitys;
    AddressCheckAdapter addressCheckAdapter;
    boolean isIsgn = true;
    int signAddress = 1;//当前的级数
    String province,
            city,
            area,
            street;
    int address1 = 0;  //当前级数的对应的id
    int address2 = 0;
    int address3 = 0;
    int address4 = 0;


    @Override
    protected FragmentServicePresenter createPresenter() {
        return new FragmentServicePresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home_service;
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//                float percent = Float.valueOf(Math.abs(verticalOffset)) / Float.valueOf(appBarLayout.getTotalScrollRange());
                //第一种
                int toolbarHeight = appBarLayout.getTotalScrollRange();
                int dy = Math.abs(verticalOffset);
                if (dy <= toolbarHeight) {
                    float scale = (float) dy / toolbarHeight;
                    float alpha = scale * 255;
                    flLayout.setBackgroundColor(Color.argb((int) alpha, 249, 140, 103));
//                    tvTitle.setTextColor(Color.argb((int) alpha, 0, 0, 0));
                }
                //第二种
            }
        });
        initLocationData();
        initTypeView();

        //广播
        mPresenter.onNewListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum);
        //分类
        mPresenter.onTypeData(LoginHelper.getLonginData().getResult(), 14, 1);
        mPresenter.onTypeTData(LoginHelper.getLonginData().getResult(), 1);
        initBusinessView();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                LoggerUtils.e("类型：" + bannerListBean.getResult().get(position).getType());
                if (bannerListBean.getResult().get(position).getType() == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", Integer.parseInt(bannerListBean.getResult().get(position).getParameter()));
                    $startActivity(IntegralCommodityDetailsActivity.class, bundle);
                } else if (bannerListBean.getResult().get(position).getType() == 2) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", "轮播图活动");
                    bundle.putString("content", bannerListBean.getResult().get(position).getParameter());
                    $startActivity(RichTextH5Activity.class, bundle);
                } else if (bannerListBean.getResult().get(position).getType() == 3) {
                    Bundle bundle = new Bundle();
                    bundle.putString("url", bannerListBean.getResult().get(position).getParameter());
                    $startActivity(RichTextWebViewH5Activity.class, bundle);
                }
            }
        });
        bannerTj.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                LoggerUtils.e("类型：" + bannerTjListBean.getResult().get(position).getType());
                if (bannerTjListBean.getResult().get(position).getType() == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", Integer.parseInt(bannerTjListBean.getResult().get(position).getParameter()));
                    $startActivity(IntegralCommodityDetailsActivity.class, bundle);
                } else if (bannerTjListBean.getResult().get(position).getType() == 2) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", "轮播图活动");
                    bundle.putString("content", bannerTjListBean.getResult().get(position).getParameter());
                    $startActivity(RichTextH5Activity.class, bundle);
                } else if (bannerTjListBean.getResult().get(position).getType() == 3) {
                    Bundle bundle = new Bundle();
                    bundle.putString("url", bannerTjListBean.getResult().get(position).getParameter());
                    $startActivity(RichTextWebViewH5Activity.class, bundle);
                }
            }
        });
        viewFlipper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //广播详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", newsListBean.getResult().getData().get(viewFlipper.getDisplayedChild()).getId());
                $startActivity(NewsInfoActivity.class, bundle);
            }
        });
        initRefreshLayout();

    }

    /**
     * 定位初始化
     */
    private void initLocationData() {
        //先判断缓存是否有。如果=null 说明没有。保存默认杭州
        //如果缓存有。判断是否是定位成功的数据。如果是。就赋值给当前
        //如果不是定位成功的数据、在判断缓存的数据是否有。如果没有就给默认的数据、
        if (LoginHelper.getLocationInfo() != null) {
            if (LoginHelper.getLocationInfo().getIsLocation() == 1) {
                b_district = LoginHelper.getLocationInfo().getDistrict();
                b_city = LoginHelper.getLocationInfo().getCity();
            } else {
                if (TextUtils.isEmpty(LoginHelper.getLocationInfo().getDistrict())) {
                    SPLocationData spLocationData = SPLocationData.nSPLocationDataInstance();
                    spLocationData.setDistrict(b_district);
                    spLocationData.setCity(b_city);
                    spLocationData.setIsLocation(0);
                    spLocationData.upSPLocationData();
                }
            }
        } else {
            SPLocationData spLocationData = SPLocationData.nSPLocationDataInstance();
            spLocationData.setDistrict(b_district);
            spLocationData.setCity(b_city);
            spLocationData.setIsLocation(0);
            spLocationData.upSPLocationData();
        }

        //todo 获取当前区
        mPresenter.onCityIdData(b_district, b_city);

    }

    /**
     * 分类 初始化
     */
    private void initTypeView() {

        typeList = new ArrayList<>();
        typeList5=new ArrayList<>();
        rvTypeList.setLayoutManager(new GridLayoutManager(getMe(), 5));
        rvTypeList.setNestedScrollingEnabled(false);
        //创建适配器
        typeAdapter = new TypeAdapter(R.layout.page_type_item_, typeList);
        //给RecyclerView设置适配器
        rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo   分类 详情
//                if (typeAdapter.getItem(position).getId() > 0) {
//                    Bundle bundle = new Bundle();
//                    bundle.putInt("sign", 1);
//                    bundle.putInt("service_type", typeAdapter.getItem(position).getId());
//                    bundle.putInt("cityID", cityIdBean.getResult().getPid());
//                    bundle.putInt("areaID", cityIdBean.getResult().getId());
//                    $startActivity(MerchantsListActivity.class, bundle);
//                } else {
//                    Bundle bundle = new Bundle();
//                    bundle.putInt("cityID", cityIdBean.getResult().getPid());
//                    bundle.putInt("areaID", cityIdBean.getResult().getId());
//                    $startActivity(TypeBusinessActivity.class, bundle);
//
//                }
                if (typeAdapter.getItem(position).getId()==-1)
                {
                    if (indexSign==1){
                        indexSign=2;
                        typeList5.get(4).setTitle("收起");
                        typeAdapter.replaceData(typeList);
                    }else {
                        indexSign=1;
                        typeList5.get(4).setTitle("展开");
                        typeAdapter.replaceData(typeList5);
                    }
                }else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("cityID", cityIdBean.getResult().getPid());
                    bundle.putInt("areaID", cityIdBean.getResult().getId());
                    bundle.putInt("id", typeAdapter.getItem(position).getId());
                    $startActivity(TypeBusinessActivity.class, bundle);
                }
            }
        });
    }

    /**
     * 推荐专区列表初始化
     */
    private void initBusinessView() {
        businessList = new ArrayList<>();
        rvBusinessList.setLayoutManager(new GridLayoutManager(getMe(), 2));
        rvBusinessList.setNestedScrollingEnabled(false);
        //创建适配器
        businessAdapter = new BusinessAdapter(R.layout.page_business_item, businessList);
        //给RecyclerView设置适配器
        rvBusinessList.setAdapter(businessAdapter);
        businessAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo  商家列表 推荐 详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", businessAdapter.getItem(position).getId());
                $startActivity(BusinessHomeActivity.class, bundle);
            }
        });


        recommendList = new ArrayList<>();
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getMe());
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager2.setSmoothScrollbarEnabled(true);
        layoutManager2.setAutoMeasureEnabled(true);
        rvRecommendList.setLayoutManager(layoutManager2);
        rvRecommendList.setNestedScrollingEnabled(false);
        //创建适配器
        recommendAdapter = new RecommendAdapter(R.layout.recommen_item, recommendList);
        //给RecyclerView设置适配器
        rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo  商家 详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", recommendAdapter.getItem(position).getId());
                $startActivity(BusinessHomeActivity.class, bundle);
            }
        });

    }

    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                //top 轮播
                mPresenter.onBannerData(LoginHelper.getLonginData().getResult(), 1);
                //推荐 轮播
                mPresenter.onBannerTjData(LoginHelper.getLonginData().getResult(), 2);
//                //广播
//                mPresenter.onNewListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum);
//                //分类
//                mPresenter.onTypeData(LoginHelper.getLonginData().getResult(), 11, 1);
                initRecommendListData();
                initBusinessData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                initBusinessData();
            }


        });
    }

    //上架列表
    private void initRecommendListData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("is_reco", 2);
        map.put("lon", LoginHelper.getLocationInfo().getLontitude());
        map.put("lat", LoginHelper.getLocationInfo().getLatitude());
        map.put("limit", 5);
        map.put("page", pagehttp);
        map.put("city", cityIdBean.getResult().getPid());
        map.put("area", cityIdBean.getResult().getId());
        map.put("fraction", "");
        map.put("juli", "");
        map.put("want", "");
        mPresenter.onRecommendListData(LoginHelper.getLonginData().getResult(), map);
    }

    //上架列表
    private void initBusinessData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("is_reco", "");
        map.put("lon", LoginHelper.getLocationInfo().getLontitude());
        map.put("lat", LoginHelper.getLocationInfo().getLatitude());
        map.put("limit", pageNum);
        map.put("page", pagehttp);
        map.put("city", cityIdBean.getResult().getPid());
        map.put("area", cityIdBean.getResult().getId());
        map.put("fraction", "");
        map.put("juli", "");
        map.put("want", 1);
        mPresenter.onBusinessListData(LoginHelper.getLonginData().getResult(), map);
    }


    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onNewListSuccess(NewListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        newsListBean = bean;
        if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
            llFlipper.setVisibility(View.VISIBLE);
            for (int i = 0; i < bean.getResult().getData().size(); i++) {
                View viewfi = getLayoutInflater().inflate(R.layout.page_notice_item, null);
                TextView name = viewfi.findViewById(R.id.tv_content);
                name.setText(bean.getResult().getData().get(i).getTitle());
                viewFlipper.addView(viewfi);
            }
            viewFlipper.setFlipInterval(2000);
            viewFlipper.startFlipping();
        } else llFlipper.setVisibility(View.GONE);
    }

    @Override
    public void onBannerSuccess(BannerListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        bannerListBean = bean;
        LoggerUtils.e("我:" + bannerListBean.toString());
        List<String> listBanner = new ArrayList<>();
        for (int i = 0; i < bean.getResult().size(); i++) {
            listBanner.add(bean.getResult().get(i).getBanner());
        }
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(listBanner);
        //动画效果
        //banner.setBannerAnimation(Transformer.Accordion);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void onBannerTjSuccess(BannerListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        bannerTjListBean = bean;
        List<String> listBanner = new ArrayList<>();
        for (int i = 0; i < bean.getResult().size(); i++) {
            listBanner.add(bean.getResult().get(i).getBanner());
        }
        bannerTj.setImageLoader(new GlideImageLoader());
        //设置图片集合
        bannerTj.setImages(listBanner);
        //动画效果
        //banner.setBannerAnimation(Transformer.Accordion);
        //banner设置方法全部调用完毕时最后调用
        bannerTj.start();
    }

    @Override
    public void onCityIdSuccess(CityIdBean bean) {
        cityIdBean = bean;
        boolean is = SPUtil.saveObjectToShare(LoginHelper.CITY_DATA, bean);
        //top 轮播
        mPresenter.onBannerData(LoginHelper.getLonginData().getResult(), 1);
        //推荐 轮播
        mPresenter.onBannerTjData(LoginHelper.getLonginData().getResult(), 2);
        initRecommendListData();
        initBusinessData();
    }

    @Override
    public void onBusinessListSuccess(BusinessListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                businessAdapter.replaceData(bean.getResult().getData());
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                businessAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @Override
    public void onRecommendListSuccess(BusinessListBean bean) {
        //todo 推荐
        recommendAdapter.replaceData(bean.getResult().getData());
    }

    @Override
    public void onTypeSuccess(IndexTypeListBean bean) {
        typeList = bean.getResult();
//        IndexTypeListBean.ResultBean resultBean = new IndexTypeListBean.ResultBean();
//        resultBean.setTitle("查看更多");
//        typeList.add(resultBean);
        if (bean.getResult().size()>5) {
            for (int i = 0; i < 5; i++) {
                typeList5.add(bean.getResult().get(i));
            }
            indexSign=1;
            typeList5.get(4).setTitle("展开");
            typeAdapter.replaceData(typeList5);
        }else {
            indexSign=2;
            typeList5.get(4).setTitle("展开");
            typeAdapter.replaceData(typeList);
        }



    }

    @Override
    public void onTypeTSuccess(IndexTypeListBean bean) {
        String isGuidPage = SPUtil.getString(LoginHelper.IS_HOME_CHEKCK);
        if (isGuidPage != null && isGuidPage.equals("1")) {
        } else {
            ApplyShopDialog applyShopDialog = new ApplyShopDialog(getMe(), bean.getResult());
            applyShopDialog.getTv_btn_next().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    applyShopDialog.dismiss();
                    String str = applyShopDialog.setCheCkStr();
                    if (!TextUtils.isEmpty(str))
                        mPresenter.onWantPushData(LoginHelper.getLonginData().getResult(), str);
                }
            });
            applyShopDialog.show();
        }
    }

    @Override
    public void onProvinceListSuccess(AddressProvinceListBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            mListCitys = bean.getResult();
            showAddress();
        }
    }

    @Override
    public void onCityListSuccess(AddressProvinceListBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            mListCitys = bean.getResult();
            addressCheckAdapter.replaceData(mListCitys);
//            if (!isIsgn)
//                addressCheckAdapter.replaceData(mListCitys);
//            else showAddress();
        }
    }

    @Override
    public void onWantPushSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        SPUtil.saveString(LoginHelper.IS_HOME_CHEKCK, "1");
    }

    @OnClick({R.id.tv_btn_search, R.id.ll_btn_address, R.id.ll_btn_right, R.id.tv_btn_business_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_search:
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 0);
//                bundle.putInt("service_type", typeRightAdapter.getItem(position).getId());
                bundle.putInt("cityID", cityIdBean.getResult().getPid());
                bundle.putInt("areaID", cityIdBean.getResult().getId());
                $startActivity(MerchantsListActivity.class, bundle);
                break;
            case R.id.ll_btn_address:
                signAddress = 1;
                mPresenter.onProvinceListData();
                break;
            case R.id.ll_btn_right:
                //todo 消息
                $startActivity(NwsMsgActivity.class);
                break;
            case R.id.tv_btn_business_more:
                bundle = new Bundle();
                bundle.putInt("sign", 0);
//                bundle.putInt("service_type", typeRightAdapter.getItem(position).getId());
                bundle.putInt("cityID", cityIdBean.getResult().getPid());
                bundle.putInt("areaID", cityIdBean.getResult().getId());
                $startActivity(MerchantsListActivity.class, bundle);
                break;
        }
    }

    /**
     * 选择地址
     */
    private void showAddress() {
        if (popupWindowAddress != null && popupWindowAddress.isShowing()) return;
        View upView = LayoutInflater.from(getMe()).inflate(R.layout.pw_address, null);
        //测量View的宽高
        CommonUtil.measureWidthAndHeight(upView);
        popupWindowAddress = new CommonPopupWindow.Builder(getMe())
                .setView(R.layout.pw_address)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .create();
        popupWindowAddress.showAtLocation(getActivity().findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            //todo
            case R.layout.pw_address:
                RecyclerView rvCoupon = view.findViewById(R.id.recyclerView);
                rvCoupon.setLayoutManager(new LinearLayoutManager(getMe()));
                TextView tv_1 = view.findViewById(R.id.tv_1);
                TextView tv_2 = view.findViewById(R.id.tv_2);
                TextView tv_3 = view.findViewById(R.id.tv_3);
                TextView tv_4 = view.findViewById(R.id.tv_4);

                if (!TextUtils.isEmpty(province)) {
                    tv_1.setText(province);
                    tv_1.setVisibility(View.VISIBLE);
                }
                if (!TextUtils.isEmpty(city)) {
                    tv_2.setVisibility(View.VISIBLE);
                    tv_2.setText(city);
                }

                if (!TextUtils.isEmpty(area)) {
                    tv_3.setVisibility(View.VISIBLE);
                    tv_3.setText(area);
                }

                if (!TextUtils.isEmpty(street)) {
                    tv_4.setVisibility(View.VISIBLE);
                    tv_4.setText(street);
                }

                tv_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(tv_2.getText().toString().toString())) {
                            tv_1.setText("");
                            tv_1.setVisibility(View.GONE);
                            signAddress = 1;
                            address1 = 0;
                            province = "";
                            tvCity.setText(LoginHelper.getCityData().getResult().getName());
                            isIsgn = false;
                            mPresenter.onCityListData(1);
                        }

                    }
                });
                tv_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(tv_3.getText().toString().toString())) {
                            tv_2.setText("");
                            tv_2.setVisibility(View.GONE);
                            address2 = 0;
                            signAddress = 2;
                            city = "";
                            isIsgn = false;
                            mPresenter.onCityListData(address1);
                        }
                    }
                });
                tv_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (TextUtils.isEmpty(tv_4.getText().toString().toString())) {
                            tv_3.setText("");
                            tv_3.setVisibility(View.GONE);
                            signAddress = 3;
                            address3 = 0;
                            area = "";
                            isIsgn = false;
                            mPresenter.onCityListData(address2);
                        }
                    }
                });
                tv_4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_4.setText("");
                        tv_4.setVisibility(View.GONE);
                        signAddress = 4;
                        address4 = 0;
                        street = "";
                        isIsgn = false;
                        mPresenter.onCityListData(address3);
                    }
                });
                //创建适配器
                addressCheckAdapter = new AddressCheckAdapter(R.layout.pw_address_item, mListCitys);
                //给RecyclerView设置适配器
                rvCoupon.setAdapter(addressCheckAdapter);
                addressCheckAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (signAddress == 1) {
                            tv_1.setVisibility(View.VISIBLE);
                            tv_1.setText(addressCheckAdapter.getItem(position).getName());
                            address1 = addressCheckAdapter.getItem(position).getId();
                            signAddress = 2;
                            province = tv_1.getText().toString().trim();
                            isIsgn = false;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else if (signAddress == 2) {
                            tv_2.setVisibility(View.VISIBLE);
                            tv_2.setText(addressCheckAdapter.getItem(position).getName());
                            address2 = addressCheckAdapter.getItem(position).getId();
                            city = tv_2.getText().toString().trim();
                            signAddress = 3;
                            isIsgn = false;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else if (signAddress == 3) {
                            tv_3.setVisibility(View.VISIBLE);
                            tv_3.setText(addressCheckAdapter.getItem(position).getName());
                            address3 = addressCheckAdapter.getItem(position).getId();
                            area = tv_3.getText().toString().trim();
//                            etAdd.setText(province + city + area);
                            signAddress = 4;
                            isIsgn = false;
                            tvCity.setText(area);
                            popupWindowAddress.dismiss();
//                            cityID=address2;
//                            areaID=address3;
                            cityIdBean.getResult().setPid(address2);
                            cityIdBean.getResult().setId(address3);

                            refreshLayout.autoRefresh();

//                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        }
//                        else {
//                            tv_4.setVisibility(View.VISIBLE);
//                            tv_4.setText(addressCheckAdapter.getItem(position).getName());
//                            address4 = addressCheckAdapter.getItem(position).getId();
//                            street = tv_4.getText().toString().trim();
////                            tvAddress.setText(province + city + area + street);
//                            tvRegion.setText(area);
//                            popupWindowAddress.dismiss();
//                        }

                    }
                });

                view.findViewById(R.id.tv_btn_colce).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindowAddress.dismiss();
                    }
                });
                break;
        }
    }
}
